package thread.chapter02;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author xinya
 * @version 1.0
 * @project javacore
 * @description 指令重排
 * @date 2023/12/13 15:28:43
 */
public class OrderSort {
    public static class ReadThread extends Thread {
        public void run() {

            while (!Thread.currentThread().isInterrupted()) {
                if (ready) {
                    System.out.println(num + num);
                }
                System.out.println("read thread ...");
            }
        }
    }

    public static class WriteThread extends Thread {
        public void run() {
            System.out.println("write thread in...");
            num = 2;
            ready = true;
            System.out.println("write thread over...");
        }
    }

    public static int num = 0;
    public static volatile boolean  ready = false;

    public static void main(String[] args) throws InterruptedException {
        ReadThread rt = new ReadThread();
        rt.start();

        WriteThread wt = new WriteThread();
        wt.start();

        Thread.sleep(10);
        rt.interrupt();

        System.out.println("main exit");
    }
}
